package com.zr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zr.model.UserModel;
import com.zr.util.JdbcUtil;

/**
 * 匹配登录数据
 * @author 曾鹏
 *
 */
public class LoginDao {
	/**
	 * 查询是否匹配数据库的数据
	 * @param usermodel 
	 * @return 如果有则返回用户的权限，1为普通用户，2为管理员用户
	 */
	public int  validateUser(UserModel usermodel){
		int flag = 0;
		String sql = "select * from user where sid = ? and spwt = ?";
		Connection con = JdbcUtil.getDBConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setInt(1, usermodel.getSid());
			pst.setString(2, usermodel.getSpwt());
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				usermodel.setSpower(rs.getInt("spower"));
			}
			flag =usermodel.getSpower();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcUtil.closedConnection(con);
		}
		return flag;
	}
	
	/**
	 * 通过传入的sid,spwt获取当前用户的信息
	 * @param sid,spwt
	 * @return 返回当前用户的信息
	 */
	public UserModel getUserInfor(int sid,String spwt){
		UserModel um = new UserModel();
		String sql = "select * from user where sid = ? and spwt =? ";
		Connection con = JdbcUtil.getDBConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setInt(1, sid);
			pst.setString(2, spwt);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				um.setSid(rs.getInt("sid"));
				um.setHid(rs.getInt("hid"));
				um.setSpower(rs.getInt("spower"));
				um.setSpwt(rs.getString("spwt"));
				um.setSname(rs.getString("sname"));
				um.setScollege(rs.getString("scollege"));
				um.setSsex(rs.getString("ssex"));
				um.setSage(rs.getInt("sage"));
				um.setSphone(rs.getString("sphone"));
				um.setSaddress(rs.getString("saddress"));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcUtil.closedConnection(con);
		}
		
		
		return um;
		
	}
	
	
	/**
	 * 通过传入的sid获取当前用户的信息
	 * @param sid
	 * @return 返回当前用户的信息
	 */
	public UserModel getUserInfor(int sid){
		UserModel um = new UserModel();
		String sql = "select * from user where sid = ? and spwt =? ";
		Connection con = JdbcUtil.getDBConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setInt(1, sid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				um.setSid(rs.getInt("sid"));
				um.setHid(rs.getInt("hid"));
				um.setSpower(rs.getInt("spower"));
				um.setSpwt(rs.getString("spwt"));
				um.setSname(rs.getString("sname"));
				um.setScollege(rs.getString("scollege"));
				um.setSsex(rs.getString("ssex"));
				um.setSage(rs.getInt("sage"));
				um.setSphone(rs.getString("sphone"));
				um.setSaddress(rs.getString("saddress"));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JdbcUtil.closedConnection(con);
		}
		
		
		return um;
		
	}

	
}

